using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using System.Reflection;

public class MiniMap : MonoBehaviour {
	public Material mapMaterial;
	public RenderTexture mapTexture;
	public Texture2D maskTexture;
	public Texture2D reflectionTexture;
	public Texture2D eyeTexture;
	public Camera fpsCamera;
	public bool displaySpawnPoints 	= false;
	public ObjectLabel objectlabel;
	public GameObject texture;
	public GameObject playerTexture;
	public RespawnHandler respawnHandler;
	
	public Vector2 mapPosition = new Vector2(75,5);
	public float mapSize = 30;
	
	
	private List<GameObject> respawnPoints;
	private int eyes	= 0;
	public bool debug	= false;
	public float minimumHeight	=	5f;
	public float maximumHeight	=	50f;
	public int zoomSteps		=	3;
	public bool rotate			= true;
	private int previousEyes	= 0;
	
	

	
	
	
	// Use this for initialization
	void Start () {
		
		foreach (FieldInfo fi in this.GetType().GetFields()){
			if (fi.FieldType == typeof(Texture2D)){
				if (fi.GetValue(this).ToString() == "null"){
					Texture2D newTexture = new Texture2D(100,100);
					newTexture.Apply();
					fi.SetValue(this,newTexture);
					if (fi.Name != "testSquare")
						Debug.LogWarning("Texture " + fi.Name + " in " + this.GetType() + " in gameojbect " + this.name + " is missing.");
				}
			}
		}	
		
		respawnHandler = GameObject.Find("EntitySpawner").GetComponent<RespawnHandler>();
		//mapTexture.format = RenderTextureFormat.ARGB32;
		
		//mapTexture.format = RenderTextureFormat.Default; 
		mapMaterial.SetTexture("_MainTex",mapTexture);
		mapMaterial.SetTexture("_Mask",maskTexture);
		//this.transform.
		if(respawnHandler != null)
			respawnPoints = respawnHandler.getRespawnPoints();
		setEyes(0);
	}
	
	// Update is called once per frame
	void Update () {
		float skipEye = Mathf.Round(Mathf.Clamp((eyes/(Mathf.Round(zoomSteps/2)))/2,0,1));
		float height = Mathf.Clamp(Vector3.up.y*((maximumHeight-minimumHeight)/(zoomSteps))*(eyes-skipEye)+minimumHeight,minimumHeight,maximumHeight)+transform.parent.transform.position.y;
		
		
		this.transform.position =  new Vector3(	this.transform.position.x,
												height,
												this.transform.position.z);
		//this.transform.rotation.x = this.transform.parent.transform.rotation.x;
		//transform.rotation = new Quaternion(0,fpsCamera.transform.rotation.y,0,0)
		//mapTexture.format = RenderTextureFormat.Default;
		if (rotate && fpsCamera != null)
		this.transform.localEulerAngles = new Vector3(transform.eulerAngles.x,fpsCamera.transform.eulerAngles.y,transform.eulerAngles.z);
		
	}
	void OnGUI(){
		float newMapSize = Mathf.Max((Screen.height*mapSize/100),(Screen.width*mapSize/100));
		GUI.DrawTexture(new Rect(Screen.width*mapPosition.x/100-newMapSize/2,Screen.height*mapPosition.y/100-newMapSize/2,newMapSize,newMapSize),eyeTexture,ScaleMode.ScaleToFit);
		 if (Event.current.type.Equals(EventType.Repaint))
			Graphics.DrawTexture(new Rect(Screen.width*mapPosition.x/100-newMapSize/2,Screen.height*mapPosition.y/100-newMapSize/2,newMapSize,newMapSize),mapTexture,mapMaterial);
		GUI.DrawTexture(new Rect(Screen.width*mapPosition.x/100-newMapSize/2,Screen.height*mapPosition.y/100-newMapSize/2,newMapSize,newMapSize),reflectionTexture,ScaleMode.ScaleToFit);
		
		if (debug){
			if (GUI.Button(new Rect(Screen.width/2-75,Screen.height-100,50,25),"-","box"))
				if (eyes > 0)
					setEyes(eyes-1);
				
			GUI.Label(new Rect(Screen.width/2-25,Screen.height-100,50,25),eyes.ToString(),"box");
			if (GUI.Button(new Rect(Screen.width/2+25,Screen.height-100,50,25),"+","box"))
				setEyes(eyes+1);

		}

	}
	
	
	void displayShootingPlayer(Transform playerTransform){
		if (eyes >= Mathf.Round(zoomSteps/2))
			objectlabel.displayTexture(playerTexture,playerTransform,1);	
	}
		
	public void setEyes(int newEyes){
		eyes =	newEyes;
		updateMinimap();
			
	/*
		if (displaySpawnPoints){
			for (int i = 0; i < respawnPoints.Count; i++){
				objectlabel.displayTexture(texture,respawnPoints[i].transform,0);
			}
		}*/
		
		
		
	}
	
	public void updateMinimap() {
		Debug.Log ("Updating minimap.");
		
		objectlabel.clearTextures();
		objectlabel.displayTexture(texture,transform.parent.transform,0);
		
		if (eyes > zoomSteps + 1){
			foreach(Entity entity in FindObjectsOfType(typeof(Entity))){
				if (entity.gameObject.layer == LayerMgr.LAYER_WORLD) {
					objectlabel.displayTexture(texture,entity.transform,0);
					Debug.Log("found entity!");
				}	
			}
		}
	}
	
	
	/*
	Texture2D maskTexture(Texture2D mask, RenderTexture texture){
		Vector2 pixelRatio = new Vector2(texture.width/mask.width,texture.height/mask.height);
		Texture2D newTexture;
		for (int i = 0; i < texture.width; i++){
			for (int j = 0; j < texture.height; j++){
				for (int k = 0; k < (int)pixelRatio.x; k++){
					if (mask.GetPixel(
				}
				
			
				
			}
		}
	}*/
}
